// pages/comment/comment.js
var app = getApp();
const { formatTime } = require("../../utils/util");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: wx.getStorageSync('user').userId,
    head_Photo: wx.getStorageSync('user').head_Photo,//头像
    names: wx.getStorageSync('user').userName,//userName 放用户名
    flag_like: false,//动态的喜欢的标志
    flag_comment: false,//初始为未点击评论
    flag_show: false,//初始为未点击
    flag_mylike: false,//评论的喜欢的标志

    //评论
    inputValue: '',

    //从数据库获取评论
    talk_temp: [],//暂存显示在当前页面的 动态信息
    talks_com: [],
  },

  /**动态喜欢 ok*/
  tolike: async function () {
    console.log(this.data.dynamicId)
    console.log(app.mylist[app.dynamicId_get-1].dynamicId)
    console.log(app.mylist[app.dynamicId_get-1])
    console.log(this.data.talk_temp)
    let ret = await new Promise((res, rej) => {
      if (this.data.dynamicId == app.mylist[app.dynamicId_get-1].dynamicId) {
        //判断是否是同一个动态
        this.data.talk_temp[0].like_dy = !this.data.talk_temp[0].like_dy
        this.setData({
          talks_temp: this.data.talk_temp
        })
      }

      // wx.request({
      //   url: 'https://api.lzjlw.top:3030/residential/community_Like?type=3',
      //   method: 'POST',
      //   data: {
      //     flag: this.data.flag_like,
      //     from_Id: this.data.dynamicId,
      //     userId: this.data.userId
      //   },
      //   success: res => {
      //     console.log(res.data)
      //     this.setData({
      //       flag_like: !this.like_dy
      //     })
      //     console.log(this.like_dy)
      //   }
      // })
    })

  },

  /**确认评论是否点赞状态 */
  like_com: false,
  likecom_status: async function (commentid) {
    let ret = await new Promise((res, rej) => {
      wx.request({
        url: `https://api.lzjlw.top:3030/residential/community_Like?type=4&&from_Id=${commentid}&&userId=${this.data.userId}`,
        success: res => {
          console.log(res.data)
          if (res.data == true) {
            this.like_com = true
          } else {
            this.like_com = false
          }
          return this.like_com
        }
      })
    })
  },
  /**评论喜欢 ok*/
  tolike_my: async function (e) {
    // console.log(e)
    // console.log(e.target.id)
    for (var i = 0; i < this.comment_get.length; i++) {
      if (this.comment_get[i].commentId == e.target.id) {
        if (!this.comment_get[i].islike) {
          this.comment_get[i].islike = true
          this.setData({
            comment_get: this.comment_get
          })
          let ret = await new Promise((res, rej) => {
            wx.request({
              url: 'https://api.lzjlw.top:3030/residential/community_Like?type=4',
              method: 'POST',
              data: {
                from_Id: e.target.id,
                userId: this.data.userId,
              },
              success: res => {
                console.log(res.data)
              }
            })
          })
          console.log(this.like_com)
        } else {
          this.comment_get[i].islike = false
          this.setData({
            comment_get: this.comment_get
          })
          let ret = await new Promise((res, rej) => {
            wx.request({
              url: 'https://api.lzjlw.top:3030/residential/community_Like?type=4',
              method: 'POST',
              data: {
                from_Id: e.target.id,
                userId: this.data.userId,
              },
              success: res => {
                console.log(res.data)
              }
            })
          })
        }
      }
    }
  },

  /**展示动态评论界面 ok*/
  tocomment: function () {
    var flag_comment = this.data.flag_comment;
    this.setData({
      flag_comment: !flag_comment,
    })
  },

  /**删除动态 ok*/
  todelete_dy: function (e) {
    wx.request({
      url: `https://api.lzjlw.top:3030/residential/community_dynamic?type=3&dynamicId=${this.data.dynamicId}`,
      method: 'DELETE',
      dataType: 'text',
      success: res => {
        if (res.statusCode == 200) {
          console.log('动态删除完成')
        }
      }
    })
    wx.switchTab({
      url: '/pages/community/community',
    })
  },
  commentId_tep: null,
  /**删除评论 ok*/
  todelete_com: async function (e) {
    //e.target.id 是评论顺序
    //console.log(e.currentTarget.id)
    //打印出点击的评论的信息
    //console.log(this.comment_get[e.currentTarget.id - 1])
    this.commentId_tep = this.comment_get[e.currentTarget.id - 1].commentId
    let res = await new Promise((res,rej)=>{
      wx.request({
        url: `https://api.lzjlw.top:3030/residential/community_dynamic?type=4&commentId=${this.commentId_tep}`,
        method: 'DELETE',
        dataType: 'text',
        success: res => {
          console.log(res.data)
          if (res.statusCode == 200) {
            let comment_gets = this.data.comment_get.filter(item => {
              return item.index != e.currentTarget.id;
            })
            this.setData({
              comment_get: comment_gets
            })
            console.log(comment_gets)
            // this.toupload_com()
            wx.navigateTo({
              url: '/pages/comment/comment',
            })
          }
        }
      })
    })
  },

  comment_get: [],
  this_commentid: null,//点击的评论id
  comms: [],
  /**加载动态下的评论 ok */
  toupload_com: async function (e) {
    /**确定获得了动态的id */
    this.data.dynamicId = app.mylist[app.dynamicId_get - 1].dynamicId
    //console.log(this.data.dynamicId)

    /**申请所有评论的信息 */
    let re = await new Promise((res, rej) => {
      wx.request({
        url: `https://api.lzjlw.top:3030/residential/community_Comments?dynamicId=${this.data.dynamicId}`,
        success: res => {
          console.log(res.data)
          /**申请所有评论的点赞状态 */
          var len = res.data.length
          for (var a = 0; a < len; a++) {
            //console.log(res.data[a].commentId)
            this.likecom_status(res.data[a].commentId)
            
            this.comms.push(this.like_com)
            console.log(this.comms)
          }
          for (var i = 0; i < len; i++) {
            // this.likecom_status(res.data[i].commentId)
            this.comment_get.unshift({
              userId:res.data[i].userId,
              islike: this.comms[i],
              index: len - i,
              head_Photo: res.data[i].head_Photo,
              dynamicId: res.data[i].dynamicId,
              commentId: res.data[i].commentId,
              userName: res.data[i].userName,
              content: res.data[i].content,
              public_Time: formatTime(new Date()),
            })
            this.setData({
              comment_get: this.comment_get,
            })
          }
          console.log(this.comment_get)
        }
      })
    })

  },

  /**失去焦点时获得 ok */
  bindInputBlur: function (e) {
    console.log(e)
    this.data.inputValue = e.detail.value;
    console.log(this.data.inputValue)
  },

  comment_up: [],
  /**发布评论 ok*/
  fabu: function () {
    console.log(this.data.inputValue)
    wx.request({
      url: `https://api.lzjlw.top:3030/residential/community_dynamic?type=4`,
      method: 'POST',
      data: {
        content: this.data.inputValue,
        dynamicId: this.data.dynamicId,
        userId: this.data.userId,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        console.log('我的评论发布成功')
      }
    })

    // wx.navigateTo({
    //   url: '/pages/comment/comment',
    // })
    this.setData({flag_comment:false})
    this.toupload_com();

  },

  /**加载动态的详情 ok*/
  like_dy: false,
  uploadfro_dy: async function () {
    //表示从哪个页面跳转进来的
    //相应的动态列表
    var index = app.dynamicId_get - 1;
    /**判断是否是user */
    var myid = wx.getStorageSync('user').userId
    if (myid != app.mylist[index].userId) {
      this.setData({
        ismy: false
      })
    } else {
      this.setData({
        ismy: true,
      })
    }
    /**判断是否带有图片 */
    if (app.mylist[index].imgUrl == 'null') {
      this.setData({
        isImg: false
      })
    } else {
      this.setData({
        isImg: true
      })
    }
    /**判断动态是否已经点赞 */
    let ret = await new Promise((resolve, ret) => {
      wx.request({
        url: `https://api.lzjlw.top:3030/residential/community_Like?type=3&&from_Id=${app.mylist[app.dynamicId_get - 1].dynamicId}&&userId=${this.data.userId}`,
        success: res => {
          // console.log(res.data)
          this.like_dy = res.data
          resolve(res)
        }
      })
    })

    this.data.talk_temp.unshift({
      dynamicId: app.mylist[index].dynamicId,
      userId: app.mylist[index].userId,
      head_Photo: app.mylist[index].head_Photo,
      userName: app.mylist[index].userName,
      content: app.mylist[index].content,
      public_Time: app.mylist[index].public_Time,
      imgUrl: app.mylist[index].imgUrl,
      flag_like: this.like_dy
    })
    this.setData({
      talks_temp: this.data.talk_temp,
    })
    app.talks_dy = this.data.talk_temp
  },

  /**
   * 生命周期函数--监听页面加载
   */
  commtStatus: [],
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.uploadfro_dy();
    this.toupload_com();
  },

  onScrollLoad: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})